package leetcode.leetcode98;

import java.util.Stack;

/**
 * @author czt
 * @version 1.0
 * @since 2025/1/2
 *
 * 判断是否为搜索二叉树
 */
public class Leetcode98 {
    public boolean isValidBST(TreeNode root) {
        // 使用中序遍历，那么他的遍历结果一定是升序的
        TreeNode node = root;
        long prev = Long.MIN_VALUE; // 上一个值
        Stack<TreeNode> stack = new Stack<>();
        while(node != null || !stack.isEmpty()){
            if(node != null){
                stack.push(node);
                node = node.left;
            }else{
                TreeNode pop = stack.pop();
                // 处理值
                if(prev >= pop.val){
                    return false;
                }
                // 更新
                prev = pop.val;
                node = pop.right;
            }
        }
        return true;
    }
}
